package com.hh.servlet.excel;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ExportExcelServlet
 */
@WebServlet("/ExportExcelServlet")
public class ExportExcelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExportExcelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  doPost(request, response);
	}

	 public void doPost(HttpServletRequest request, HttpServletResponse response)

             throws ServletException, IOException {

      request.setCharacterEncoding("UTF-8");//设置request的编码方式，防止中文乱码

      String fileName ="导出数据.xlsx";//设置导出的文件名称

      StringBuffer sb = new StringBuffer(request.getParameter("tableInfo"));//将表格信息放入内存

      String contentType = "application/vnd.ms-excel";//定义导出文件的格式的字符串

      String recommendedName = new String(fileName.getBytes(),"iso_8859_1");//设置文件名称的编码格式

      response.setContentType(contentType);//设置导出文件格式

      response.setHeader("Content-Disposition", "attachment; filename=newfile.xlsx");//

      response.resetBuffer();

      //利用输出输入流导出文件

      ServletOutputStream sos = response.getOutputStream();

      sos.write(sb.toString().getBytes());

      sos.flush();

      sos.close();

}

}
